%*********************************************************/
% FUNCTION:draw_image
% FILE NAME:draw_image.m
% VERSION:1.0
% DATE:Jun 16, 2010
% AUTHOR:Dong Wang
% CODE TYPE:
% DESCRIPTION:This function is used to draw colorful picture in Y, U, V
% with grid
% USAGE:
% draw_image(y, u, v, grid_size, grid_color, mask, mask_color_indx, fade_coef);
%   grid_size   =   [gx, gy];
%   grid_color  =   [y, u, v] or [y] or []
%   fade_coef   =   x   x in [0 ~ 1]
%   mask        =   color indx of object mask
%   mask_color_indx =   color value of object

% ALL RIGHT RESERVED.
%-----------------------------
% $Header: $
% $Author: dongw $
% $Date: $
% $Revision: 1.4 $
%------------------------------
% MODIFICATION HISTORY:
% CVS Revision History
% $Log: draw_image.m,v $
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

function draw_image(y, u, v, grid_size, grid_color, mask, mask_color_indx, fade_coef)
[sx, sy] = size(y);

%fade
if(isempty(mask))
    y	=	y*fade_coef + (128 - 128 * fade_coef);
end

yuv    =   zeros([sy, sx, 3]);
yuv(:,:,1)     =   y';  yuv(:,:,2)  =   u';  yuv(:,:,3)  =v';

%grid
color_length    =   length(grid_color);
b_grid          =   isempty(grid_size);
b_grid_mask     =   length(grid_size)>2;
if(b_grid_mask)
    temp = yuv(:, :, 1);
    temp(grid_size==1) = grid_color(1);
    yuv(:, :, 1) = temp;
    temp = yuv(:, :, 2);
    temp(grid_size==1) = grid_color(2);
    yuv(:, :, 2) = temp;
    temp = yuv(:, :, 3);
    temp(grid_size==1) = grid_color(3);
    yuv(:, :, 3) = temp;
elseif(~b_grid)
	switch (color_length)
        case    3, 
            for( ix = 1 : grid_size(1) : sx)
                yuv(:, ix, 1) = grid_color(1);
                yuv(:, ix, 2) = grid_color(2);
                yuv(:, ix, 3) = grid_color(3);
            end
	    for( iy = 1 : grid_size(2) : sy)
                yuv(iy, :, 1) = grid_color(1);
                yuv(iy, :, 2) = grid_color(2);
                yuv(iy, :, 3) = grid_color(3);
	    end
        case    1,
            for( ix = 1 : grid_size(1) : sx)
                yuv(:, ix, 1) = grid_color;
            end
            for( iy = 1 : grid_size(2) : sy)
                yuv(iy, :, 1) = grid_color;
            end
        case 2, display('unsupported grid_color number');
        otherwise;
	end
end

rgb    =   ycbcr2rgb(uint8(yuv));

if(~isempty(mask))
    r   =   double(rgb(:,:,1));
    g   =   double(rgb(:,:,2));
    b   =   double(rgb(:,:,3));
    
    r_color_indx    =   mask_color_indx(:,1);
    g_color_indx    =   mask_color_indx(:,2);
    b_color_indx    =   mask_color_indx(:,3);
    
    r_mask =   r_color_indx(mask'+1);
    g_mask =   g_color_indx(mask'+1);
    b_mask =   b_color_indx(mask'+1);
    
    r   =   fade_coef*r + (1-fade_coef) * r_mask;
    g   =   fade_coef*g + (1-fade_coef) * g_mask;
    b   =   fade_coef*b + (1-fade_coef) * b_mask;
    
    rgb(:,:,1)  =   uint8(r);
    rgb(:,:,2)  =   uint8(g);
    rgb(:,:,3)  =   uint8(b);
    
end

figure;imshow(rgb);
